import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {PmMessage, PmMessageDeal} from "@shared/models/PmMessage";
import {TestData} from "@shared/utils/TestData";
import {api} from "@core/service/config/api";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {Result} from "@shared/models/Result";
import {NzMessageService} from "ng-zorro-antd/message";

@Injectable({
  providedIn: 'root',
})
export class PmMessageService {
  // 权限请求列表
  list: PmMessage[]=[];
  constructor(
    protected message: NzMessageService,
    private http: HttpClient,
    ) {
  }

  flash() {
    for (let item of TestData.pmReq) {
      item.avatar=api.getAvatar+'/'+item.id
    }
    BBYUtils.handelResult(
      this.http.get<Result>(`${api.messageList}?action=receive&all=true`,BBYUtils.createHeadersOption()),
      res => {
        res.data.forEach((item:any) => {
          item.avatar = api.getAvatar + "/" + item.from
        })
        this.list = res.data
      },
      res => {
        this.message.error(res.msg)
      }
    )
  }

  delete(id: string) {
    BBYUtils.handelResult(
      this.http.get<Result>(`${api.delMessage}?messageId=${id}`,BBYUtils.createHeadersOption()),
      res => {
        this.message.success('删除完成！')
        this.flash();
      },
      res => {
        this.message.error(res.msg)
      }
    )
  }

  deal(data:PmMessageDeal) {
    return this.http.post<Result>(api.dealMessage,data,BBYUtils.createHeadersOption())
  }
}
